<template>
	<view>
		<!-- 地址列表 -->
		<view class="content">
			<block v-for="(item, index) in addressList" :key="index">
				<view class="list" @longtap="toShowSheet(index)">
					<view class="wrapper" @click="toChooseAddress(index)">
						<view class="u-box">
							<text class="name text-lg text-bold text-black">{{ item.username }}</text>
							<text class="mobile text-lg text-bold text-black">{{ item.mobile }}</text>
						</view>
						<view class="address-box">
							<block v-if="item.isDefault==1" >
								<u-tag text="默认" style="margin-top:10rpx;margin-right: 10rpx;" size="mini" type="error" shape="circle" mode="light" />
							</block>
							<text class="text-gray text-sm">{{ item.addressName + ' ' + item.area }}</text>
						</view>
					</view>
					<u-icon name="edit-pen" size="35" @click="toResetAddress(item.id)" />
				</view>
			</block>
			<text style="display:block;padding:16rpx 30rpx 10rpx;lihe-height:1.6;color:#fa436a;font-size:24rpx;"></text>
			<!-- <button class="add-btn" @click="toAddressManagePage">新增地址</button> -->
			<view class="login__info__item__button tn-cool-bg-color-15--reverse" hover-class="tn-hover"
				:hover-stay-time="150"
				@click="toAddressManagePage">新增地址</view>
		</view>
		<!-- 加载 -->
		<u-action-sheet v-model="show" :list="actions" cancel-text="取消" :cancel-btn="true" @click="onSelect"
			@close="onClose" />
		<!-- //消息弹窗 -->
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	const db = uniCloud.database();
	//引入全局变量
	var app = getApp();
	export default {
		data() {
			return {
				index: 0,
				userinfo: {},
				openid: '',
				addressList: [],
				loading: false,
				show: false,
				actions: [{
					text: '编辑',
					color: 'blue',
					fontSize: 28
				}, {
					text: '删除',
					color: 'blue',
					fontSize: 28
				}, {
					text: '复制',
					color: 'blue',
					fontSize: 28
				}],
				school:-1
			};
		},
		onShow() {
			let that = this;
			//返回用户信息和id
			if (this.openid == undefined || this.openid == '') {
				uni.showModal({
					title: "您的登录缓存已经过期，需要重新登录",
					success(res) {
						uni.switchTab({
							url: "../me/me"
						})
					}
				})
			}else{
				if(this.school==-1){
					this.getAddressList();
				}else{
					this.getAddressListBySchool(Number(this.school));
				}
			}
		},
		onLoad(params) {
			//TODO:先弄成虚假openid
			this.openid = params.openid
			if( params.school==undefined){
			}else{
				this.school = params.school
			}
			if (this.openid == undefined || this.openid == '') {
				uni.showModal({
					title: "您的登录缓存已经过期，需要重新登录",
					success(res) {
						uni.switchTab({
							url: "../me/me"
						})
					}
				})
			}else{
			
			}
			// this.openid = "494848"
		},
		methods: {
			//选中返回
			toChooseAddress(index) {
				app.globalData.shuaxinAddressID = this.addressList[index].id;
				setTimeout(function() {
					uni.navigateBack({})
				}, 800);
			},
			//长按
			toShowSheet(index) {
				console.log("长按选中", index);
				this.show = true;
				this.index = index;
			},
			//底部选项框
			onClose() {
				this.show = false;
			},
			//点击底部弹窗选项
			onSelect(index) {
				let that = this;
				if (index == 0) {
					//编辑
					uni.navigateTo({
						url: `addressManage/addressManage?id=${this.addressList[index].id}&openid=` + that.openid
					})
				} else if (index == 1) {
					//删除地址
					uni.request({
						url: "https://zback.shixiaozhu.work/ShixiaozhuTest/address/deteleAddress?addressId=" + that.addressList[that
							.index].id,
						success(res) {
							if (res.data.status == 200) {
								that.getAddressList();
								that.$refs.uToast.show({
									title: '成功删除！',
									type: 'success'
								});
							}else{
								that.$refs.uToast.show({
									title: '网络异常！',
									type: 'error'
								});
							}
						}

					})
				} else if (index == 2) {
					//复制  

				}
			},
			//修改地址
			toResetAddress(id) {
				console.log(id)
				uni.navigateTo({
					url: `addressManage/addressManage?id=${id}&openid=` + this.openid,
				})
			},
			//新增地址
			toAddressManagePage() {
				
				uni.navigateTo({
					url: 'addressManage/addressManage?openid=' + this.openid
				});
			},
			//请求地址列表
			getAddressList() {
				var that = this;
				//开始请求地址
				//TODO:先弄成虚假openid
				console.log("开始请求")
				uni.request({
					url: "https://zback.shixiaozhu.work/ShixiaozhuTest/address/getAddress?openid="+this.openid,
					// url: "https://zback.shixiaozhu.work/ShixiaozhuTest/address/getAddress?openid=494848",
					success(res) {
						that.addressList = res.data.data;
					}
				})
			},
			//请求地址列表
			getAddressListBySchool(school) {
				var that = this;
				//开始请求地址
				//TODO:先弄成虚假openid
				console.log("开始请求")
				uni.request({
					url: "https://zback.shixiaozhu.work/ShixiaozhuTest/address/getAddressBySchool?openid="+this.openid+"&school="+Number(school),
					// url: "https://zback.shixiaozhu.work/ShixiaozhuTest/address/getAddress?openid=494848",
					success(res) {
						that.addressList = res.data.data;
					}
				})
			}
		}
	};
</script>

<style>
	page {
		padding-bottom: 120rpx;
		background: #f6f7fb;
	}
	
	.login__info__item__button {
		position: fixed;
		left: 30rpx;
		right: 30rpx;
		bottom: 16rpx;
		z-index: 95;
		width: 690rpx;
		height: 80rpx;
		padding: 10rpx;
		border-radius: 5px;
		text-align: center;
		font-size: 32rpx;
		font-weight: bold;
		justify-content: center;
		align-items: center;
		margin-bottom: 20rpx;
	}
	

	.content {
		position: relative;
	}

	.list {
		display: -webkit-box;
		display: -webkit-flex;
		display: flex;
		-webkit-box-align: center;
		-webkit-align-items: center;
		align-items: center;
		padding: 20rpx 30rpx;
		background: #fff;
		position: relative;
	}

	.wrapper {
		display: -webkit-box;
		display: -webkit-flex;
		display: flex;
		-webkit-box-orient: vertical;
		-webkit-box-direction: normal;
		-webkit-flex-direction: column;
		flex-direction: column;
		-webkit-box-flex: 1;
		-webkit-flex: 1;
		flex: 1;
	}

	.address-box {
		display: -webkit-box;
		display: -webkit-flex;
		display: flex;
		-webkit-box-align: center;
		-webkit-align-items: center;
		align-items: center;
	}

	.address-box .tag {
		font-size: 24rpx;
		color: #fa436a;
		margin-right: 10rpx;
		background: #fffafb;
		border: 1px solid #ffb4c7;
		border-radius: 4rpx;
		padding: 4rpx 10rpx;
		line-height: 1;
	}

	.address-box .address {
		font-size: 30rpx;
		color: #303133;
	}

	.u-box {
		font-size: 28rpx;
		color: #909399;
		margin-top: 16rpx;
	}

	.u-box .name {
		margin-right: 30rpx;
	}

	.icon-bianji {
		display: -webkit-box;
		display: -webkit-flex;
		display: flex;
		-webkit-box-align: center;
		-webkit-align-items: center;
		align-items: center;
		height: 80rpx;
		font-size: 40rpx;
		color: #909399;
		padding-left: 30rpx;
	}

	.add-btn {
		position: fixed;
		left: 30rpx;
		right: 30rpx;
		bottom: 16rpx;
		z-index: 95;
		display: -webkit-box;
		display: -webkit-flex;
		display: flex;
		-webkit-box-align: center;
		-webkit-align-items: center;
		align-items: center;
		-webkit-box-pack: center;
		-webkit-justify-content: center;
		justify-content: center;
		width: 690rpx;
		height: 80rpx;
		font-size: 32rpx;
		color: #fff;
		background-color: #fa436a;
		border-radius: 10rpx;
		-webkit-box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
